package com.devunion.bogdan.view.dialogs;

import com.devunion.bogdan.view.util.i18n;
import com.devunion.bogdan.view.util.DialogHelper;
import com.devunion.bogdan.view.util.ComponentFactory;
import com.devunion.bogdan.view.actions.ActionManager;
import com.devunion.bogdan.view.actions.annotations.AddTextAnnotationAction;
import com.devunion.bogdan.view.actions.annotations.AddImageAnnotationAction;
import com.devunion.bogdan.view.actions.annotations.RemoveAnnotationAction;
import com.devunion.bogdan.conf.Config;
import com.devunion.bogdan.model.project.dao.UserDao;
import com.devunion.bogdan.model.project.User;
import com.devunion.bogdan.model.project.UserRole;
import com.devunion.bogdan.model.UserManager;
import com.devunion.bogdan.spring.Context;

import javax.swing.*;
import java.awt.*;

/**
 * @author Victar Liaskovich
 */
public class LoginDialog extends JDialog {
    private UserDao userDao;

    public LoginDialog(Frame owner) {
        super(owner, i18n.getString("LoginDialog.Title"));

        initComponents();
    }

    private void initComponents() {
        getContentPane().setLayout(new GridBagLayout());

        final JTextField jTextFieldUser = new JTextField(10);
        final JPasswordField jPasswordField = new JPasswordField(10);

        JLabel jLabelUser = new JLabel(i18n.getString("LoginDialog.Login"));
//        jLabelUser.setDisplayedMnemonic(i18n.getKeystroke("JCheckBoxPanelDemo.User.Keystroke"));
        jLabelUser.setLabelFor(jTextFieldUser);

        DialogHelper.placeComponent(getContentPane(), jLabelUser,
                0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.NORTHEAST, GridBagConstraints.NONE,
                10, 10, 10, 0);

        jTextFieldUser.setText(Config.getLogin());
        DialogHelper.placeComponent(getContentPane(), jTextFieldUser,
                1, 0, 1, 1, 0.1, 0.0, GridBagConstraints.NORTHWEST, GridBagConstraints.HORIZONTAL,
                10, 0, 10, 0);

        JLabel jLabelPassword = new JLabel(i18n.getString("LoginDialog.Password"));
//        jLabelPassword.setDisplayedMnemonic(i18n.getKeystroke("JCheckBoxPanelDemo.Password.Keystroke"));
        jLabelPassword.setLabelFor(jPasswordField);

        DialogHelper.placeComponent(getContentPane(), jLabelPassword,
                0, 1, 1, 1, 0.0, 0.0, GridBagConstraints.NORTHEAST, GridBagConstraints.NONE,
                10, 10, 10, 0);

        jPasswordField.setText(Config.getPassword());
        DialogHelper.placeComponent(getContentPane(), jPasswordField,
                1, 1, 1, 1, 0.1, 0.0, GridBagConstraints.NORTHEAST, GridBagConstraints.HORIZONTAL,
                10, 0, 10, 0);

        DialogHelper.placeComponent(getContentPane(), ComponentFactory.getHR(),
                0, 2, 2, 1, 1.0, 0.0, GridBagConstraints.NORTHWEST, GridBagConstraints.HORIZONTAL,
                10, 10, 10, 0);

        Component OkCancelPanel = ComponentFactory.createPanelButtonOkCancel(this, new ComponentFactory.ActionHandler() {
            public void execute() {
                Config.setLogin(jTextFieldUser.getText());
                Config.setPassowrd(new String(jPasswordField.getPassword()));
                doLogin(jTextFieldUser.getText(), new String(jPasswordField.getPassword()));
                setVisible(false);
            }
        }, new ComponentFactory.ActionValidator() {
            public boolean validate() {
                return true;
            }
        });

        DialogHelper.placeComponent(getContentPane(), OkCancelPanel,
                0, 3, 2, 1, 1.0, 0.0, GridBagConstraints.NORTHEAST, GridBagConstraints.HORIZONTAL,
                10, 10, 10, 10);
    }

    private void doLogin(String login, String pass) {
        if (userDao == null) {
            userDao = (UserDao) Context.getContext().getBean("userDao");
        }

        User user = userDao.getUser(login, pass);

        if (user == null) {
            JOptionPane.showMessageDialog(this, "There is no user with specified login and password", "Login", JOptionPane.WARNING_MESSAGE);
            return;
        }

        UserManager.getInstance().setUser(user);
    }
}
